layui.define(['table', 'form', 'upload', 'admin'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , upload = layui.upload
        , admin = layui.admin;

    //用户列表
    table.render({
        elem: '#LAY-user-manage'
        , url: config.user_getlist
        , cols: [[
            {field: 'id', width: 100, title: 'ID', sort: true, fixed: 'left'}
            , {field: 'account', title: '账号', width: 150}
            , {field: 'mobile', title: '手机号', width: 150}
            , {
                field: 'nick_name', title: '昵称', minWidth: 150, templet: function (d) {
                    return decodeURI(d.nick_name);
                }
            }
            , {
                field: '', title: '头像', width: 80, templet: function (d) {
                    if (d.avatar) {
                        return '<img style="display: inline-block; height: 100%;" src= "' + d.avatar + '">'
                    }
                    return "无";
                }
            }
            // , {field: 'anchor_point', title: '主播经验值', width: 100}
            , {
                field: 'gender', width: 80, title: '性别', templet: function (d) {
                    if (!d.profile) {
                        return '未知';
                    }
                    switch (d.profile.gender) {
                        case 1:
                            return '男';
                        case 2:
                            return '女';
                        default:
                            return "未知";
                    }
                }
            }
            , {field: 'gold', width: 80, title: '钻石'}
            , {field: 'diamond', width: 120, title: '宝克'}
            , {
                field: '', width: 120, title: '所属经纪人', templet: function (d) {
                    return d.guild_manager ? d.guild_manager.real_name : '';
                }
            }
            , {
                field: 'online_status', width: 100, title: '在线状态', templet: function (d) {
                    switch (d.online_status) {
                        case 1:
                            return '<span class="layui-badge layui-bg-green">在线</span>';
                        case 2:
                            return '<span class="layui-badge layui-bg-orange">暂离</span>';
                        case 3:
                            return '<span class="layui-badge layui-bg-red">通话中</span>';
                        case 9:
                            return '<span class="layui-badge layui-bg-black">离线</span>';
                        default:
                            return "";
                    }
                }
            }
            , {field: 'regist_time', width: 200, title: '创建时间'}
            , {
                field: '', width: 100, title: '分成比例', templet: function (d) {
                    return d.sharing_ratio + "%";
                }
            }
            , {
                field: '', width: 100, title: '状态', templet: function (d) {
                    switch (d.status) {
                        case 0:
                            return '<span class="layui-badge layui-bg-green">有效</span>';
                        case 1:
                            return '<span class="layui-badge layui-bg-orange">封禁</span>';
                        default:
                            return "";
                    }
                }
            }
            , {title: '操作', width: 300, align: 'center', fixed: 'right', toolbar: '#table-userlist-operation'}
        ]]
        , page: true
        , limit: 20
        , height: 'full-180'
        , text: '对不起，加载出现异常！'
        , even: true
    });

    //监听工具条
    table.on('tool(LAY-user-manage)', function (obj) {
        if (obj.event === 'rescind') {
            layer.confirm('确定解约用户？', function (index) {
                admin.req({
                    url: config.user_rescind
                    , data: {"id": obj.data.id}
                    , success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-user-manage");
                            layer.msg(res.msg, {icon: 1});
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    }
                });
            });
        } else if (obj.event === 'setSharingRatio') {
            layer.prompt({
                formType: 0,
                value: obj.data.sharing_ratio,
                title: '分成比例',
            }, function (value, index, elem) {
                admin.req({
                    url: config.user_set_sharing_ratio
                    , data: {'id': obj.data.id, 'sharing_ratio': value}
                    , success: function (res) {
                        if (res.code == 0) {
                            layer.close(index);
                            layer.msg(res.msg, {icon: 1});
                            table.reload('LAY-user-manage');
                        }
                    }
                });
            });
        } else if (obj.event === 'setManager') {
            index = layer.open({
                type: 2,
                title: "设置经纪人",
                content: config.user_edit_manager + "?id=" + obj.data.id,
                maxmin: true,
                area: ["600px", "550px"],
            });
            layer.full(index);
        }
    });


    //查看照片
    admin.events.photosPreview = function (othis) {
        let urls = othis.attr("value");
        let urlarr = new Array();
        $.each(urls.split(","), function (i, val) {
            urlarr.push({"src": val});
        });
        layer.photos({
            photos: {
                "title": "查看照片" //相册标题
                , "data": urlarr
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    };

    form.on("submit(user-add)", function (obj) {
        var data = obj.field;
        layer.load();
        //提交
        admin.req({
            url: config.user_add_post,
            data: data,
            success: function (res) {
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 1});
                    window.location.reload();
                }
                layer.closeAll("loading");
            },
            done: function () {
                layer.closeAll("loading");
            },
        });
    });


    exports('guild/user', {})
});